home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / mint / utils / initsnb.zoo / init / write / write.c < prev    next >
C/C++ Source or Header  |  1992-01-22  |  2KB  |  127 lines

  1. /*
  2.  * write - send a message to another user
  3.  * (C) 1991 D P Gymer
  4.  * This code may be freely redistributed under the terms of the GNU GPL.
  5.  *
  6.  * $Log$
  7.  */
  8.  
  9. #include <fcntl.h>
  10. #include <signal.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <termcap.h>
  15. #include <unistd.h>
  16. #include <utmp.h>
  17.  
  18. const char *rcsid = "$Id$";
  19.  
  20. #define UTMP    "/etc/utmp"
  21.  
  22. static void
  23. do_write(line)
  24.     char *line;
  25. {
  26.     int fd,
  27.     c;
  28.     char host[16];
  29.     FILE *fp;
  30.  
  31.     if (gethostname(host, 16))
  32.     strcpy(host, "unknown-host");
  33.     signal(SIGTSTP, SIG_IGN);
  34.     if ((fd = open(line, O_WRONLY, 0)) < 0 || !(fp = fdopen(fd, "w"))) {
  35.     perror(line);
  36.     exit(1);
  37.     }
  38.     fprintf(fp, "Message from %s@%s on %s...\n", getlogin(), host, ttyname(-1));
  39.     while ((c = getchar()) != EOF)
  40.     fputc(c, fp);
  41.     fputs("EOF\n", fp);
  42.     fclose(fp);
  43.     signal(SIGTSTP, SIG_DFL);
  44. }
  45.  
  46. static char *
  47. findline(user, line)
  48.     char *user;
  49.     char *line;
  50. {
  51.     static char buf1[14];    /* Magic number alert! */
  52.     char buf2[9];
  53.     int fd;
  54.     struct utmp utmp;
  55.  
  56.     if (!line) {
  57.     if ((fd = open(UTMP, O_RDONLY, 0)) < 0) {
  58.         perror(UTMP);
  59.         exit(1);
  60.     }
  61.     while (read(fd, &utmp, sizeof(struct utmp)) == sizeof(struct utmp)) {
  62.         if (!strncmp(utmp.ut_name, user, 8)) {    /* Magic #! */
  63.         if (line) {
  64.             printf("User logged in more than once; trying %s...\n",
  65.             line);
  66.         } else {
  67.             line = buf2;
  68.             strncpy(buf2, utmp.ut_line, 8);
  69.             buf2[8] = '\0';
  70.         }
  71.         } else if (!strncmp(utmp.ut_line, line, 8)) {
  72.         fprintf(stderr, "%s is not logged to that terminal!\n", user);
  73.         exit(1);
  74.         }
  75.     }
  76.     close(fd);
  77.     }
  78.     if (line) {
  79.     sprintf(buf1, "/dev/%s", line);
  80.     line = buf1;
  81.     }
  82.     return line;
  83. }
  84.  
  85. static void
  86. usage()
  87. {
  88.     fprintf(stderr, "Usage: write [tty] user\n");
  89.     exit(1);
  90. }
  91.  
  92. int
  93. main(argc, argv)
  94.     int argc;
  95.     char **argv;
  96. {
  97.     int c;
  98.     char *user,
  99.        *line;
  100.     extern int opterr,
  101.         optind;
  102.  
  103.     opterr = 0;
  104.     while ((c = getopt(argc, argv, "")) != EOF)
  105.     switch (c) {
  106.     default:
  107.         usage();
  108.     }
  109.  
  110.     if (optind == argc - 2)
  111.     line = argv[optind++];
  112.     else
  113.     line = 0;
  114.  
  115.     if (optind != argc - 1)
  116.     usage();
  117.  
  118.     if (!(line = findline((user = argv[optind]), line))) {
  119.     fprintf(stderr, "%s is not logged on!\n", user);
  120.     exit(1);
  121.     }
  122.  
  123.     do_write(line);
  124.  
  125.     return 0;
  126. }
  127.